using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model;
using ZR.Admin.WebApi.MainModules;
using ZR.ServiceCore.Attributes;

namespace ZR.Admin.WebApi.Controllers.BaseFrame
{
    /// <summary>
    /// 角色
    /// </summary>
    public class RoleControllerBase : ApiControllerBase
    {

        /// <summary>
        /// 角色接口
        /// </summary>
        protected readonly IRoleService _RoleService;

        protected readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public RoleControllerBase(IRoleService roleService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _RoleService = roleService;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [ActionPermissionFilter(Permission = "role:add")]
        [Log(Title = "角色", BusinessType = BusinessType.INSERT)]
        public virtual async Task AddRole([FromBody] RoleFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<RoleEntity>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            var isOk = await _RoleService.AddRole(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "添加失败");
            }
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <returns></returns>
        [HttpPost("modify")]
        [ActionPermissionFilter(Permission = "role:edit")]
        [Log(Title = "角色", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ModifyRole([FromBody] RoleFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<RoleEntity>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            var isOk = await _RoleService.ModifyRole(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "role:delete")]
        [Log(Title = "角色", BusinessType = BusinessType.DELETE)]
        public virtual async Task DeleteRole([FromBody] DeleteRoleReq req)
        {
            var idArr = Tools.SplitAndConvert<long>(req.Ids);
            var isOk = await _RoleService.DeleteRole(idArr, false);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "删除失败");
            }
        }

        /// <summary>
        /// 变更角色状态
        /// </summary>
        /// <returns></returns>
        [HttpPost("changeState")]
        [ActionPermissionFilter(Permission = "role:changestate")]
        [Log(Title = "角色", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ChangeRoleState([FromBody] ChangeRoleStateReq req)
        {
            var isOk = await _RoleService.ChangeRoleState(req);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }


        /// <summary>
        /// 导出角色列表
        /// </summary>
        /// <returns></returns>
        [Log(Title = "角色", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("exportList")]
        [ActionPermissionFilter(Permission = "role:exportList")]
        [IgnoreRspContract]
        public virtual async Task<IActionResult> ExportRoleList([FromQuery] GetRolePageListReq req)
        {
            req.PageNum = 1;
            req.PageSize = 100000;
            var listInfo = await _RoleService.GetRolePageList(req);
            if (listInfo.Result == null || listInfo.Result.Count <= 0)
            {
                throw new CustomException(ResultCode.NO_DATA,"没有要导出的数据");
            }
            var result = await ExportExcelMiniAsync(listInfo.Result, "角色", "角色");
            return ExportExcel(result.Item2, result.Item1);
        }




        /// <summary>
        /// 查询角色分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "role:list")]
        public virtual async Task<PagedInfo<RoleListItemDto>> GetRolePageList([FromBody] GetRolePageListReq req)
        {
            var result = await _RoleService.GetRolePageList(req);
            return result;
        }

        /// <summary>
        /// 查询角色详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "role:detail")]
        public virtual async Task<RoleDetailDto> GetRoleDetail(long id)
        {
            var result = await _RoleService.GetRoleDetail(id);
            return result;
        }

    }
}